package com.ruoyi.applet.controller;

import java.time.LocalDateTime;
import java.util.List;
import java.io.IOException;
import java.util.Random;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.applet.domain.SpikesInfo;
import com.ruoyi.applet.domain.vo.UserAddress;
import com.ruoyi.applet.service.IAddressService;
import com.ruoyi.applet.service.IOrderService;
import com.ruoyi.applet.service.ISpikesInfoService;
import com.ruoyi.applet.utli.TokenByUsrId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.applet.domain.Spikes;
import com.ruoyi.applet.service.ISpikesService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 秒杀活动Controller
 * 
 * @author ruoyi
 * @date 2024-11-14
 */
@RestController
@RequestMapping("/applet")
public class SpikesController extends BaseController
{
    @Resource
    private ISpikesService spikesService;

    @RequestMapping("/spikes")
    public Object selectSpikes(){
        return spikesService.selectAllByGoodsIdSpikesGoodsList();
    }

    @RequestMapping("/spikesByOrder")
    public Object selectSpikesOrder(@RequestParam("productId")Long productId){
        return spikesService.selectBySpikesIdList(productId);
    }

    @RequestMapping("/spikesByProductId")
    public Object selectSpikesProduct(@RequestParam("productId")Long productId){
        return spikesService.selectByProductIdList(productId);
    }

    @RequestMapping("/spikesTime")
    public Object selectSpikesTime(){
        LocalDateTime now = LocalDateTime.now();
        return spikesService.selectAllByCreateTimeSpikesList(now);
    }

    @Resource
    IAddressService addressService;

    @Resource
    ISpikesInfoService spikesInfoService;

    @Resource
    IOrderService orderService;

    @RequestMapping("/spikesInsert")
    public Object insertSpikesByUserId(@RequestParam("token") String token,@RequestParam("orderId") String orderId,@RequestParam("orderTotal") String orderTotal,
                                      @RequestParam("orderGoodsId") Long orderGoodsId, @RequestParam("orderNumber") Long orderNumber,@RequestParam("spikesInfoId")Long spikesInfoId) {
        UserAddress address = addressService.selectAllByAddressUsrId(TokenByUsrId.tokenByUsrId(token));
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 18; i++) {
            sb.append(random.nextInt(10)); // 生成0到9之间的随机数字
        }
        if (spikesService.insertAllInteger(TokenByUsrId.tokenByUsrId(token), orderId, orderTotal, address.getUsrName(), address.getAddressPhone(), address.getAddressDesc() + address.getAddressText(), orderGoodsId, orderNumber, sb.toString(),spikesInfoId) == 1) {
            SpikesInfo spikesInfo = spikesInfoService.selectAllBySpikesGoodIdSpikesInfo(orderGoodsId, spikesInfoId);
            if (spikesInfo.getSpikesNumber() > orderNumber) {
                spikesInfoService.updateSpikesNumber(orderGoodsId, spikesInfo.getSpikesNumber() - orderNumber, spikesInfo.getSpikesSold() + orderNumber, spikesInfoId);
                return "0";
            }
        }
        return "1";
    }

    @RequestMapping("/spikesByUserId")
    public Object selectSpikesByUserId(@RequestParam("token") String token,@RequestParam("spikesGoodId") Long spikesGoodId){
       return orderService.selectOrderNotesAndOrderUerId(TokenByUsrId.tokenByUsrId(token),spikesGoodId) != null;
    }
}
